<?php
    include(dirname(__FILE__)."/../../../Dao/db.php");

    function removeCampaign($idArray)
    {
        $campaignIds = split(",",$idArray);

        $ids = "(";
        for($i=0;$i<count($campaignIds)-1;$i++)
        {
            $ids .=$campaignIds[$i].",";
        }
        $ids .= $campaignIds[count($campaignIds)-1].")";

        $query = "UPDATE campaign SET status=9,lastUpdate=now(),";
        $query .= "sync=CASE WHEN campaignId IS NULL THEN 1 ELSE 2 END ";
        $query .= "WHERE camp_Id IN $ids";
        $result = mysql_query($query);
        if ($result != "1")
        {
            $message = "削除失敗しました";
        }
        else
        {
            //該当キャンペーンの広告グループ、クリエイティブ、キーワード全部で削除
            $query = "UPDATE adgroup SET status=9,sync=1,lastUpdate=now() ";
            $query .= "WHERE camp_Id IN $ids";
            $result1 = mysql_query($query);

            $query = "UPDATE creative SET status=9,sync=1,lastUpdate=now() ";
            $query .= "WHERE camp_Id IN $ids";
            $result2 = mysql_query($query);

            $query = "UPDATE keyword SET status=9,sync=1,lastUpdate=now() ";
            $query .= "WHERE camp_Id IN $ids";
            $result3 = mysql_query($query);

            if ($result1 != "1" || $result2 != "1" || $result3 != "1")
            {
                $message = "削除失敗しました";
            }
            else
            {
                $message =  "削除しました";
            }
        }
        return $message;
    }

    function removeAdGroup($idArray)
    {
        $adGroupIds = split(",",$idArray);

        $ids = "(";
        for($i=0;$i<count($adGroupIds)-1;$i++)
        {
            $ids .=$adGroupIds[$i].",";
        }
        $ids .= $adGroupIds[count($adGroupIds)-1].")";

        //9は削除状態
        $query = "UPDATE adgroup SET status=9,lastUpdate=now(),";
        $query .= "sync=CASE WHEN adGroupId IS NULL THEN 1 ELSE 2 END ";
        $query .= "WHERE adGr_Id IN $ids";
        $result = mysql_query($query);
        if ($result != "1")
        {
            $message = "削除失敗しました";
        }
        else
        {
            $query = "UPDATE creative SET status=9,sync=1,lastUpdate=now() ";
            $query .= "WHERE adGr_Id IN $ids";
            $result1 = mysql_query($query);

            $query = "UPDATE keyword SET status=9,sync=1,lastUpdate=now() ";
            $query .= "WHERE adGr_Id IN $ids";
            $result2 = mysql_query($query);

            if ($result1 != "1" || $result2 != "1")
            {
                $message = "削除失敗しました";
            }
            else
            {
                $message =  "削除しました";
            }
        }
        return $message;
    }

    function removeCreative($idArray)
    {
        $creativeIds = split(",",$idArray);

        $ids = "(";
        for($i=0;$i<count($creativeIds)-1;$i++)
        {
            $ids .=$creativeIds[$i].",";
        }
        $ids .= $creativeIds[count($creativeIds)-1].")";

        //9は削除状態
        $query = "UPDATE creative SET status=9,lastUpdate=now(),";
        $query .= "sync=CASE WHEN creativeId IS NULL THEN 1 ELSE 2 END ";
        $query .= "WHERE Crt_Id IN $ids";
        $result = mysql_query($query);
        if ($result != "1")
        {
            $message = "削除失敗しました";
        }
        else
        {
            $message = "削除しました";
        }

        return $message;
    }

    //キーワードを削除
    function removeKeyword($idArray)
    {
        $keywordIds = split(",",$idArray);

        $ids = "(";
        for($i=0;$i<count($keywordIds)-1;$i++)
        {
            $ids .=$keywordIds[$i].",";
        }
        $ids .= $keywordIds[count($keywordIds)-1].")";

        //9は削除状態
        $query = "UPDATE keyword SET status=9,lastUpdate=now(),";
        $query .= "sync=CASE WHEN keywordId IS NULL THEN 1 ELSE 2 END ";
        $query .= "WHERE key_Id IN $ids";
        $result = mysql_query($query);
        if ($result != "1")
        {
            $message = "削除失敗しました";
        }
        else
        {
            $message = "削除しました";
        }
        return $message;
    }

?>
